package top.lc951.myandroid.views.card;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import top.lc951.bannerview2.transform.ScaleInTransformer;
import top.lc951.myandroid.R;
import top.lc951.myandroid.views.GifView;

public abstract class BaseOverlayPageAdapter<T> extends PagerAdapter {
    private Context context;

    public Context getContext() {
        return context;
    }

    public List<T> getList() {
        return list;
    }

    private List<T> list = Collections.emptyList();

    public void setList(List<T> list) {
        this.list = list;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public BaseOverlayPageAdapter(Context context) {
        this.context = context;
    }

    /**
     * item布局
     *
     * @return
     */
    protected abstract View itemView(int position);

    public void setImgUrlsAndBindViewPager(ViewPager vp, List<T> imgUrls, int layerAmount) {
        setImgUrlsAndBindViewPager(vp, imgUrls, layerAmount, -1, -1);
    }

    /**
     * @param vp
     * @param imgUrls
     * @param layerAmount 显示层数
     */
    public void setImgUrlsAndBindViewPager(ViewPager vp, List<T> imgUrls, int layerAmount, float scaleOffset, float transOffset) {
        this.list = imgUrls;
        if (imgUrls != null && imgUrls.size() > 0) {
            vp.setOffscreenPageLimit(layerAmount);
//            OverlayTransformer transformer=new OverlayTransformer(layerAmount, scaleOffset, transOffset);
//            OverlayTransformerPoratior transformer=new OverlayTransformerPoratior(0.65f);
//            OverlayTransformerLan transformer=new OverlayTransformerLan(layerAmount,0.85f);
            OverlayTransformerLan2 transformer=new OverlayTransformerLan2(0.85f);
//            CardOverlayTransformer transformer=new CardOverlayTransformer();
            vp.setPageTransformer(true, transformer);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        final T itemObj = list.get(position);
        View rootView = itemView(position);
        if (null == rootView) {
            throw new RuntimeException("you should set a item layout");
        }
        ImageView imageView = rootView.findViewById(R.id.card_iv);
        if (null == imageView) {
            throw new RuntimeException("you should set a item layout");
        }
        container.addView(rootView);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = "";
        String date = "";

        LinearLayout ll_content = rootView.findViewById(R.id.ll_content);
        LinearLayout ll_status = rootView.findViewById(R.id.ll_status);
        GifView living=rootView.findViewById(R.id.living);
        TextView tv_status = rootView.findViewById(R.id.tv_status);
        TextView tv_time = rootView.findViewById(R.id.tv_time);
        living.setVisibility(View.GONE);
        living.setPaused(false);
        tv_time.setText(time);

        ll_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClicked(position);
                }
            }
        });
        return rootView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener clickListener) {
        this.mOnItemClickListener = clickListener;
    }

    public interface OnItemClickListener {
        void onItemClicked(int position);
    }
}
